package com.linkai.多线程._06_syn;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Description: 线程不安全的集合 ArrayList
 * @Author: 林凯
 * @Date: 2021/10/19 22:29
 */
public class UnsafeList {
    public static void main(String[] args) throws InterruptedException {
        List<String> list = new ArrayList<>();

        for (int i = 0; i < 10000; i++) {
            new Thread(()->{
                synchronized (list) {
                    list.add(Thread.currentThread().getName());
                }
//                try {
//                    TimeUnit.MILLISECONDS.sleep(10);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                list.add(Thread.currentThread().getName());
            }).start();
        }
        while (Thread.activeCount() > 2) {
        }
        System.out.println(list.size());
    }
}
